from django.http import JsonResponse
from Web.WebClassCongregation import UserInfo,ActiveScanList,MedusaQuery
from ClassCongregation import ScanInformation,ErrorLog
import json
from Web.Workbench.LogRelated import UserOperationLogRecord,RequestLogRecord

"""ActiveScanListQuery
{
	"token": "XXXXX"
}
"""

def ActiveScanListQuery(request):#主动扫描列表查询
    RequestLogRecord(request, request_api="active_scan_list_query")
    if request.method == "POST":
        try:
            UserToken=json.loads(request.body)["token"]
            Uid = UserInfo().QueryUidWithToken(UserToken)  # 如果登录成功后就来查询用户名
            if Uid!=None: # 查到了UID
                UserOperationLogRecord(request, request_api="active_scan_list_query", uid=Uid)
                ActiveScanListQueryResult=ActiveScanList().Query(uid=Uid)
                if ActiveScanListQueryResult!=None:
                    return JsonResponse({'message':ActiveScanListQueryResult, 'code': 200, })
                else:
                    return JsonResponse({'message': '数据库出问题了🐈', 'code': 404, })
        except Exception as e:
            ErrorLog().Write("Web_Api_VulnerabilityQuery_ActiveScanListQuery(def)", e)
            return JsonResponse({'message': '莎酱被玩坏啦(>^ω^<)喵', 'code': 500, })
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })



"""ScanInformationQuery
{
	"token": "XXXXX",
	"sid":"1"
}
"""
def ScanInformationQuery(request):#查询关系表
    RequestLogRecord(request, request_api="scan_information_query")
    if request.method == "POST":
        try:
            UserToken=json.loads(request.body)["token"]
            Sid=json.loads(request.body)["sid"]
            Uid = UserInfo().QueryUidWithToken(UserToken)  # 如果登录成功后就来查询用户名
            if Uid!=None: # 查到了UID
                UserOperationLogRecord(request, request_api="scan_information_query", uid=Uid)
                MedusaQueryResult=ScanInformation().Query(uid=Uid,sid=Sid)
                if MedusaQueryResult!=None:
                    return JsonResponse({'message':MedusaQueryResult, 'code': 200, })
                else:
                    return JsonResponse({'message': '数据库出问题了🐈', 'code': 404, })
        except Exception as e:
            ErrorLog().Write("Web_Api_VulnerabilityQuery_ScanInformationQuery(def)", e)
            return JsonResponse({'message': '莎酱被玩坏啦ヽ(･ω･´ﾒ)', 'code': 500, })
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })

"""MedusaValueQuery
{
	"token": "XXXXX",
	"ssid":"1"
}
"""
def MedusaValueQuery(request):#查询单个漏洞
    RequestLogRecord(request, request_api="medusa_value_query")
    if request.method == "POST":
        try:
            UserToken=json.loads(request.body)["token"]
            Ssid=json.loads(request.body)["ssid"]
            Uid = UserInfo().QueryUidWithToken(UserToken)  # 如果登录成功后就来查询用户名
            if Uid!=None: # 查到了UID
                UserOperationLogRecord(request, request_api="medusa_value_query", uid=Uid)
                MedusaQueryResult=MedusaQuery().Query(uid=Uid,ssid=Ssid)
                if MedusaQueryResult!=None:
                    return JsonResponse({'message':MedusaQueryResult, 'code': 200, })
                else:
                    return JsonResponse({'message': '数据库出问题了🐈', 'code': 404, })
        except Exception as e:
            ErrorLog().Write("Web_Api_VulnerabilityQuery_MedusaValueQuery(def)", e)
            return JsonResponse({'message': '莎酱被玩坏啦ヾ(=･ω･=)o', 'code': 500, })
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })